package com.fatcat.easybuy.service.impl;

import com.fatcat.easybuy.mapper.EasyBuyProductCategoryMapper;
import com.fatcat.easybuy.mapper.EasyBuyProductMapper;
import com.fatcat.easybuy.pojo.EasyBuyProductCategory;
import com.fatcat.easybuy.pojo.EasyBuyProductCategoryDTO;
import com.fatcat.easybuy.pojo.EasyBuyProductCategoryNode;
import com.fatcat.easybuy.service.EasyBuyProductCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;
import java.util.stream.Collectors;
import java.util.Collections;
import java.util.List;


/**
* @author Fatcat
* @description 针对表【easybuy_product_category】的数据库操作Service实现
* @createDate 2025-09-10 11:07:47
*/
@Service
public class EasyBuyProductCategoryServiceImpl implements EasyBuyProductCategoryService{
    @Autowired
    private EasyBuyProductCategoryMapper easyBuyProductCategoryMapper;
    @Autowired
    private EasyBuyProductMapper easyBuyProductMapper;

    @Override
    public List<EasyBuyProductCategoryDTO> getCategoryTree() {
        // 1. 从数据库获取所有分类
        List<EasyBuyProductCategory> allCategories = easyBuyProductCategoryMapper.getAllProductCategory();

        // 2. 构建内存中的树形结构
        List<EasyBuyProductCategoryNode> treeNodes = buildTree(allCategories);

        // 3. 转换为DTO结构
        return convertToDto(treeNodes);
    }

    @Override
    public List<EasyBuyProductCategoryNode> buildTree(List<EasyBuyProductCategory> categories) {
        Map<Long, EasyBuyProductCategoryNode> nodeMap = new HashMap<>();
        List<EasyBuyProductCategoryNode> roots = new ArrayList<>();

        // 第一遍：创建所有节点
        for (EasyBuyProductCategory cat : categories) {
            EasyBuyProductCategoryNode node = new EasyBuyProductCategoryNode(cat);
            nodeMap.put(cat.getId(), node);
        }

        // 第二遍：建立父子关系
        for (EasyBuyProductCategory cat : categories) {
            EasyBuyProductCategoryNode node = nodeMap.get(cat.getId());
            if (cat.getParentId() == 0) {
                roots.add(node);
            } else {
                EasyBuyProductCategoryNode parent = nodeMap.get((long)cat.getParentId());
                if (parent != null) {
                    parent.addChild(node);
                }
            }
        }

        return roots;
    }

    @Override
    public List<EasyBuyProductCategoryDTO> convertToDto(List<EasyBuyProductCategoryNode> nodes) {
        return nodes.stream().map(this::convertNode).collect(Collectors.toList());
    }

    @Override
    public EasyBuyProductCategoryDTO convertNode(EasyBuyProductCategoryNode node) {
        EasyBuyProductCategoryDTO dto = new EasyBuyProductCategoryDTO();
        dto.setId(node.getId());
        dto.setName(node.getName());
        dto.setType(node.getType());

        if (!node.getChildren().isEmpty()) {
            dto.setChildren(convertToDto(node.getChildren()));
        }

        return dto;
    }

    @Override
    public boolean addCategory(EasyBuyProductCategory easyBuyProductCategory) {
        //判断同分类下是否存在相同名字的分类
        if (easyBuyProductCategoryMapper.findByName(easyBuyProductCategory.getName(), easyBuyProductCategory.getParentId()) != null) {
            return false;
        }
        return easyBuyProductCategoryMapper.addEasyBuyProductCategory(easyBuyProductCategory)==1;
    }

    @Override
    public boolean updateCategory(EasyBuyProductCategory easyBuyProductCategory) {
        return easyBuyProductCategoryMapper.updateEasyBuyProductCategory(easyBuyProductCategory)==1;
    }

    @Override
    public boolean deleteCategory(Long id) {
        return easyBuyProductCategoryMapper.deleteEasyBuyProductCategory(id) == 1;
    }
    // 获取所有一级分类
    public List<EasyBuyProductCategory> getLevel1Categories() {
        return easyBuyProductCategoryMapper.selectLevel1Categories();
    }

    @Override
    public List<EasyBuyProductCategory> getCategoriesByParentId(Long parentId) {
        return easyBuyProductCategoryMapper.selectChildrenByParentId(parentId);
    }


    // 根据父ID获取子分类（二级/三级）
    public List<EasyBuyProductCategory> getChildrenByParentId(Long parentId) {
        if (parentId == null) {
            throw new IllegalArgumentException("父分类ID不能为空");
        }
        return easyBuyProductCategoryMapper.selectChildrenByParentId(parentId);
    }

    @Override
    public boolean updateProductLevel(Long categoryLevel1Id,Long categoryLevel2Id,Long categoryLevel3Id,EasyBuyProductCategory easyBuyProductCategory) {
        int affectedRows = easyBuyProductMapper.updateProductLevel(
                categoryLevel1Id, categoryLevel2Id, categoryLevel3Id, easyBuyProductCategory.getId()
        );

        return affectedRows >= 0;
    }
}




